<template>
<div class="index">
  <head-top></head-top>
  <div class="info">
    <a href="javascript:;" class="file">
      <input id="upload" type="file" @change="importfxx(this)"  accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />
    <el-button type="primary" @click="upload">上传</el-button>
    </a>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="姓名" width="180">
      </el-table-column>
      <el-table-column prop="phone" label="电话">
      </el-table-column>
    </el-table>
  </div>
</div>
</template>

<script>
import headTop from "@/components/headTop";
import {userSave} from "../server/api.js";
import { Promise } from 'q';
export default {
  name: "index",
  components: {
    headTop
  },
  data() {

    return {
       tableData: []
    }
    

  },
  methods: {
      upload(){
        let arrPromise = []
        
        if(this.tableData.length == 0){
         this.open4()
        }else{
          this.tableData.forEach(item => {
          arrPromise.push(userSave({
            name:item.name,
            phone:item.phone
          }))
        });
        Promise.all(arrPromise).then(res=>{
          // console.log(res)
        }) 
         this.open1()
        }
        
      },
       open1() {
        this.$notify({
          title: '成功',
          message: '上传成功',
          type: 'success'
        });
      },
       open4() {
        this.$notify.error({
          title: '错误',
          message: '请选择文件'
        });
      },
     importfxx(obj) {
                let _this = this;
                let inputDOM = this.$refs.inputer;
                // 通过DOM取文件数据
                this.file = event.currentTarget.files[0];
                var rABS = false; //是否将文件读取为二进制字符串
                var f = this.file;
                var reader = new FileReader();
                //if (!FileReader.prototype.readAsBinaryString) {
                FileReader.prototype.readAsBinaryString = function(f) {
                    var binary = "";
                    var rABS = false; //是否将文件读取为二进制字符串
                    var pt = this;
                    var wb; //读取完成的数据
                    var outdata;
                    var reader = new FileReader();
                    reader.onload = function(e) {
                        var bytes = new Uint8Array(reader.result);
                        var length = bytes.byteLength;
                        for(var i = 0; i < length; i++) {
                            binary += String.fromCharCode(bytes[i]);
                        }
                        var XLSX = require('xlsx');
                        if(rABS) {
                            wb = XLSX.read(btoa(fixdata(binary)), { //手动转化
                                type: 'base64'
                            });
                        } else {
                            wb = XLSX.read(binary, {
                                type: 'binary'
                            });
                        }
                        outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);//outdata就是你想要的东西
                        
                        // outdata = outdata.map(val=>{
                        //     Object.keys(val).forEach(key=>{
                        //         val[key] = val[key].replace(/\s*/g,"")
                        //         val[key] = val[key].replace(/<\.+?>/g,"")
                        //         val[key] = val[key].replace(/[\r\n]/g, "")
                        //     })
                        //     return val
                        // })
                        _this.tableData = outdata
                        console.log(outdata)                        
                    }
                    reader.readAsArrayBuffer(f);
                }
                if(rABS) {
                    reader.readAsArrayBuffer(f);
                } else {
                    reader.readAsBinaryString(f);
                }
            },
  }
};
</script>
<style lang="less">
.index {
}
</style>


